package com.qf.rbac.controller.api;

import com.qf.rbac.common.result.R;
import com.qf.rbac.common.result.ResponseEnum;
import com.qf.rbac.config.JwtConfig;
import com.qf.rbac.entity.User;
import com.qf.rbac.entity.dto.LoginDTO;
import com.qf.rbac.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@CrossOrigin("*")
@RequestMapping("/api/user")
public class ApiUserController {

    @Autowired
    private UserService userService;

    @Autowired
    private JwtConfig jwtConfig;

    /**
     * 用户密码登录
     * @param userName
     * @param password
     * @return
     */
    @PostMapping("/login")
    public R<LoginDTO> login(String userName, String password){
        User user = userService.login(userName, password);
        LoginDTO loginDTO = new LoginDTO();
        BeanUtils.copyProperties(user, loginDTO);
        // 生成token
        String token = jwtConfig.generateJwt(loginDTO);

        loginDTO.setToken(token);
        return new R(ResponseEnum.SUCCESS, loginDTO);
    }

}
